package com.gdk.ksoft.ui.mores;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.provider.MediaStore;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.assembly.utils.system.PhoneUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 建议反馈页面
 */
public class ExplainActivity extends Activity {

    public static final String[] EXPLAIN_TYPES = {"数据下载问题","数据表问题","上传问题","下单问题","登录问题","其他问题"};//反馈类型

    @ViewInject(R.id.pexplain_etTitle)private EditText etTitle;//标题文字
    @ViewInject(R.id.pexplain_txtType)private TextView txtType;//反馈类型
    @ViewInject(R.id.pexplain_etContent)private TextView etContent;//反馈内容
    @ViewInject(R.id.pexplain_img1)private ImageView imgUpload1;//需要上传的图片
    @ViewInject(R.id.pexplain_img2)private ImageView imgUpload2;//需要上传的图片
    @ViewInject(R.id.pexplain_img3)private ImageView imgUpload3;//需要上传的图片

    private int selectedExplainType = 0;//被选中的反馈问题类型
    private int imgWidthHeight = 0,whichImageIndex = 0;//当前选中的是那一张图片

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_explain);
        ViewUtils.inject(this);
        imgWidthHeight = (PhoneUtils.getScreenInfo(this).widthPixels-30)/3;//每张图片应该的宽度和高度
    }

    @OnClick(value = {R.id.pexplain_img1,R.id.pexplain_img2,R.id.pexplain_img3,
            R.id.pexplain_txtSubmit,R.id.pexplain_txtType})
    public void onViewClick(View v){
        switch(v.getId()){
            case R.id.pexplain_img1:
                whichImageIndex = 0;
                pickImage();//进行图片选择
                break;
            case R.id.pexplain_img2:
                whichImageIndex = 1;
                pickImage();//进行图片选择
                break;
            case R.id.pexplain_img3:
                whichImageIndex = 2;
                pickImage();//进行图片选择
                break;
            case R.id.pexplain_txtType:
                showExplainTypeDialog();//选择反馈类型
                break;
            case R.id.pexplain_txtSubmit:
                Toast.makeText(this,"暂未开放，请稍候!",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    /**
     * 进行图片选择
     */
    public void pickImage(){
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 0x0001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x0001 && resultCode == Activity.RESULT_OK && data!=null)
        {
            try {
                Uri uri = data.getData();
                Cursor cursor = this.getContentResolver().query(uri, null, null, null, null);
                if (cursor.moveToFirst()) {
                    String filename = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
                    Bitmap bmp = BitmapFactory.decodeFile(filename);
                    bmp = ThumbnailUtils.extractThumbnail(bmp, imgWidthHeight, imgWidthHeight);
                    if (bmp != null) {
                        switch (whichImageIndex) {
                            case 0:
                                imgUpload1.setImageBitmap(bmp);
                                break;
                            case 1:
                                imgUpload2.setImageBitmap(bmp);
                                break;
                            case 2:
                                imgUpload3.setImageBitmap(bmp);
                                break;
                        }
                    }
                }
                cursor.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 选择反馈类型的选择框
     */
    public void showExplainTypeDialog(){
        new AlertDialog.Builder(this).setItems(EXPLAIN_TYPES,new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                txtType.setText(EXPLAIN_TYPES[which]);
                selectedExplainType = which;
            }
        }).show();
    }

    @OnClick(value={R.id.pexplain_txtBack})
    public void onBackClick(View view){
        this.finish();
        overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
            overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
